package org.liaohailong.library.widget.shopping;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import org.liaohailong.library.widget.shopping.adapter.ShoppingAdapter;
import org.liaohailong.library.widget.shopping.adapter.ShoppingSectionAdapter;
import org.liaohailong.library.widget.shopping.adapter.ShoppingViewHolder;
import org.liaohailong.library.widget.shopping.layout.ShoppingLayoutManager;
import org.liaohailong.library.widget.shopping.observable.IShoppingDataObserver;

import java.util.LinkedList;
import java.util.List;

/**
 * Author: Victor
 * E-mail: liaohailong190@foxmail.com
 * Date: 2019/6/22 14:54
 * Description: 电商购物平台首页列表
 */
public class ShoppingListView extends ViewGroup implements IShoppingDataObserver {

    /**
     * 适配器
     */
    private ShoppingAdapter<?> mAdapter;
    /**
     * 布局器
     */
    private List<ShoppingLayoutManager> mLayoutManagers = new LinkedList<>();

    public ShoppingListView(Context context) {
        this(context, null);
    }

    public ShoppingListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShoppingListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setAdapter(ShoppingAdapter adapter) {
        if (mAdapter == adapter) {
            return;
        }
        if (mAdapter != null) {
            mAdapter.setDataObserver(null);
        }
        mAdapter = adapter;
        mAdapter.setDataObserver(this);
        mLayoutManagers.clear();
        for (ShoppingSectionAdapter<?> sectionAdapter : mAdapter.getSectionAdapters()) {
            ShoppingLayoutManager layoutManager = sectionAdapter.getLayoutManager();
            mLayoutManagers.add(layoutManager);
        }
        requestLayout();
    }

    public ShoppingAdapter getAdapter() {
        return mAdapter;
    }

    public ShoppingViewHolder getChildViewHolder(View view) {
        if (view.getParent() != null) {
            LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
            return layoutParams.viewHolder;
        }
        return null;
    }

    @Override
    public void detachViewFromParent(View child) {
        super.detachViewFromParent(child);
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }

    @Override
    public void onItemRangeChanged(int position, int count) {

    }

    @Override
    public void onItemRangeInserted(int position, int count) {

    }

    @Override
    public void onItemRangeRemoved(int position, int count) {

    }


    public final class LayoutParams extends ViewGroup.LayoutParams {

        private ShoppingViewHolder viewHolder;

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams p) {
            super(p);
        }

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
        }

        public ShoppingViewHolder getViewHolder() {
            return viewHolder;
        }

        public void setViewHolder(ShoppingViewHolder viewHolder) {
            this.viewHolder = viewHolder;
        }
    }
}
